In [1]:
require 'daru/view'


Install the spreadsheet gem version ~>1.1.1 for using spreadsheet functions.

Install the mechanize gem version ~>2.7.5 for using mechanize functions.
Out[1]:
true

In [2]:
Daru::View.plotting_library = :googlecharts


Out[2]:
:googlecharts

Basic example


In [10]:
idx = Daru::Index.new ['City', '2010 Population',]
data_rows = [
['New York City, NY', 8175000],
        ['Los Angeles, CA', 3792000],
        ['Chicago, IL', 2695000],
        ['Houston, TX', 2099000],
        ['Philadelphia, PA', 1526000]
]
df_city_pop = Daru::DataFrame.rows(data_rows)
df_city_pop.vectors = idx
df_city_pop


Out[10]:
Daru::DataFrame(5x2)
City 2010 Population
0 New York City, NY 8175000
1 Los Angeles, CA 3792000
2 Chicago, IL 2695000
3 Houston, TX 2099000
4 Philadelphia, PA 1526000

In [12]:
bar_basic_table = Daru::View::Table.new(df_city_pop)
bar_basic_table.show_in_iruby


Out[12]:

In [16]:
bar_basic_options = {
  title: 'Population of Largest U.S. Cities',
  type: :bar,
}
bar_basic_chart = Daru::View::Plot.new(bar_basic_table.table, bar_basic_options)
bar_basic_chart.show_in_iruby


Out[16]:

In [18]:
bar_basic_options = {
    title: 'Population of Largest U.S. Cities',
    chartArea: {width: '50%'},
    hAxis: {
          title: 'Total Population',
          minValue: 0
        },
    vAxis: {
          title: 'City'
        },
    type: :bar,
  height: 400
}
bar_basic_chart = Daru::View::Plot.new(bar_basic_table.table, bar_basic_options)
bar_basic_chart.show_in_iruby


Out[18]:

Bar chart With Styling


In [19]:
# Note: If youwant to use role, label, pattern, etc column, then directly use the google datatable 
# (don't create the daru dataframe/vector, since daru dataframe vectors can be string or number. So can't be a hash )
# Refer this link for more datatable options : https://developers.google.com/chart/interactive/docs/reference
data = {
  cols: [{id: 'Element', label: 'Element', type: 'string'},
          {id: 'Density', label: 'Density', type: 'number'},
          {type: 'string', role: "style" },
           ],
  rows: [{c:[{v: 'Copper'}, {v: 8.94}, {v: '#b87333'}]},
           {c:[{v: 'Silver'}, {v: 10.49}, {v: 'silver'}]},
    {c:[{v: 'Gold'}, {v: 19.30}, {v: 'gold'}]},
           {c:[{v: 'Platinum'}, {v: 21.45}, {v: '#e5e4e2'}]}]
  }
bar_table = Daru::View::Table.new(data, height: 300, width: 200)
bar_table.show_in_iruby


Out[19]:

In [20]:
bar_options = {
title: "Density of Precious Metals, in g/cm^3",
        width: 600,
        height: 400,
        bar: {groupWidth: "95%"},
        legend: { position: "none" },
   type: :bar,
  height: 400
}
bar_chart = Daru::View::Plot.new(bar_table, bar_options)
bar_chart.show_in_iruby


(pry):173: warning: key :height is duplicated and overwritten on line 177
Out[20]:

In [23]:
data = {
  cols: [{id: 'Element', label: 'Element', type: 'string'},
          {id: 'Density', label: 'Density', type: 'number'},
          {type: 'string', role: "style" },
        ],
  rows: [
    {c:[{v: 'Copper'}, {v: 8.94}, {v: 'color: gray'}]},
    {c:[{v: 'Silver'}, {v: 10.49}, {v: 'color: #76A7FA'}]},
    {c:[{v: 'Gold'}, {v: 19.30}, {v: 'opacity: 0.2'}]},
    {c:[{v: 'Platinum'}, {v: 21.45}, {v: 'stroke-color: #703593; stroke-width: 4; fill-color: #C5A5CF'}]},
    {c:[{v: 'Other'}, {v: 31.45}, {v: 'stroke-color: #871B47; stroke-opacity: 0.6; stroke-width: 8; fill-color: #BC5679; fill-opacity: 0.2'}]}
    ]
  }
bar_table = Daru::View::Table.new(data, height: 300, width: 200)
bar_table.show_in_iruby


Out[23]:

In [24]:
bar_options = {
title: "Density of Precious Metals, in g/cm^3",
        width: 600,
        height: 400,
        bar: {groupWidth: "95%"},
        legend: { position: "none" },
   type: :bar,
  height: 400
}
bar_chart = Daru::View::Plot.new(bar_table, bar_options)
bar_chart.show_in_iruby


(pry):222: warning: key :height is duplicated and overwritten on line 226
Out[24]:

In [27]:
data = {
  cols: [{id: 'Element', label: 'Element', type: 'string'},
          {id: 'Density', label: 'Density', type: 'number'},
          {type: 'string', role: "style" },{type: 'string', role: "annotation" }
           ],
  rows: [{c:[{v: 'Copper'}, {v: 8.94}, {v: '#b87333'}, {v: 'Cu'}]},
           {c:[{v: 'Silver'}, {v: 10.49}, {v: 'silver'}, {v: 'Ag'}]},
    {c:[{v: 'Gold'}, {v: 19.30}, {v: 'gold'}, {v: 'Au'}]},
           {c:[{v: 'Platinum'}, {v: 21.45}, {v: '#e5e4e2'}, {v: 'Pt'}]}]
  }
bar_table = Daru::View::Table.new(data)
bar_table.show_in_iruby


Out[27]:

In [28]:
bar_options = {
title: "Density of Precious Metals, in g/cm^3",
        width: 600,
        height: 400,
        bar: {groupWidth: "95%"},
        legend: { position: "none" },
   type: :bar,
  height: 400
}
bar_chart = Daru::View::Plot.new(bar_table, bar_options)
bar_chart.show_in_iruby


(pry):268: warning: key :height is duplicated and overwritten on line 272
Out[28]:

Stacked bar charts


In [29]:
data = {
  cols: [{id: 'Genre', label: 'Genre', type: 'string'},
          {id: 'Fantasy & Sci Fi', label: 'Fantasy & Sci Fi', type: 'number'},
          {id: 'Romance', label: 'Romance', type: 'number'},
          {id: 'Mystery/Crime', label: 'Mystery/Crime', type: 'number'},
          {id: 'General', label: 'General', type: 'number'},
          {id: 'Western', label: 'Western', type: 'number'},
          {id: 'Literature', label: 'Literature', type: 'number'},
          {type: 'string', role: "annotation" }
           ],
  rows: [{c:[{v: '2010'}, {v: 10}, {v: 24}, {v: 20}, {v: 32}, {v: 18}, {v: 5}, {v: ''}]},
        {c:[{v: '2020'}, {v: 16}, {v: 22}, {v: 23}, {v: 30}, {v: 16}, {v: 9}, {v: ''}]},
        {c:[{v: '2030'}, {v: 28}, {v: 19}, {v: 29}, {v: 30}, {v: 12}, {v: 13}, {v: ''}]},
    ]
  
  }
bar_stacked_table = Daru::View::Table.new(data)
bar_stacked_table.show_in_iruby


Out[29]:

In [32]:
bar_stacked_options = {
        width: 600,
        height: 400,
        legend: { position: 'top', maxLines: 3 },
        bar: { groupWidth: '75%' },
        isStacked: true,
   type: :bar,
}
bar_stacked_chart = Daru::View::Plot.new(bar_stacked_table, bar_stacked_options)
bar_stacked_chart.show_in_iruby


Out[32]:

In [34]:
# Same data in array of rows. Not including annotation role
data = [
  ['Genre', 'Fantasy & Sci Fi', 'Romance', 'Mystery/Crime', 'General',
         'Western', 'Literature' ],
        ['2010', 10, 24, 20, 32, 18, 5],
        ['2020', 16, 22, 23, 30, 16, 9],
        ['2030', 28, 19, 29, 30, 12, 13]
  ]
bar_stacked_table = Daru::View::Table.new(data)
bar_stacked_table.show_in_iruby


Out[34]:

In [38]:
bar_stacked_options = {
        width: 600,
        height: 400,
        isStacked: true,
   type: :bar,
}
bar_stacked_chart = Daru::View::Plot.new(bar_stacked_table, bar_stacked_options)
bar_stacked_chart.show_in_iruby


Out[38]:

In [40]:
# Percent
bar_stacked_options = {
        width: 600,
        height: 400,
        isStacked: 'percent',
   type: :bar,
}
bar_stacked_chart = Daru::View::Plot.new(bar_stacked_table, bar_stacked_options)
bar_stacked_chart.show_in_iruby


Out[40]:

In [42]:
# We can manage the x axis ticks
bar_stacked_options = {
        width: 600,
        height: 400,
        isStacked: 'percent',
   type: :bar,
  hAxis: {
            minValue: 0,
            ticks: [0, 0.3, 0.6, 0.9, 1]
          }
}
bar_stacked_chart = Daru::View::Plot.new(bar_stacked_table, bar_stacked_options)
bar_stacked_chart.show_in_iruby


Out[42]:

TODO : Work on Material bar charts . It is defined in google_visualr: https://developers.google.com/chart/interactive/docs/gallery/barchart


In [44]:
# Same data in array of rows. Not including annotation role
data = [
  ['Year', 'Sales', 'Expenses', 'Profit'],
          ['2014', 1000, 400, 200],
          ['2015', 1170, 460, 250],
          ['2016', 660, 1120, 300],
          ['2017', 1030, 540, 350]
  ]
bar_multiple_table = Daru::View::Table.new(data)
bar_multiple_table.show_in_iruby


Out[44]:

In [49]:
# We can manage the x axis ticks
bar_multiple_options = {
chart: {
            title: 'Company Performance',
            subtitle: 'Sales, Expenses, and Profit: 2014-2017',
          },
          bars: 'horizontal',
  type: :bar, height: 400, widht: 400
}
bar_multiple_chart = Daru::View::Plot.new(bar_multiple_table.table, bar_multiple_options)
bar_multiple_chart.show_in_iruby


Out[49]:

In [66]:
# Same data in array of rows. Not including annotation role
data = [
  ['Galaxy', 'Distance', 'Brightness'],
          ['Canis Major Dwarf', 8000, 230.3],
          ['Sagittarius Dwarf', 24000, 4000.5],
          ['Ursa Major II Dwarf', 30000, 1412.3],
          ['Lg. Magellanic Cloud', 50000, 120.9],
          ['Bootes I', 60000, 1223.1]
  ]
bar_multiple_table = Daru::View::Table.new(data)
bar_multiple_table.show_in_iruby


Out[66]:

In [67]:
# We can manage the x axis ticks
bar_multiple_options = {
width: 800,

            title: 'Nearby galaxies',
            subtitle: 'distance on the left, brightness on the right',
          bars: 'horizontal', # Required for Material Bar Charts.
          series: {
            0 => { axis: 'brightness' }, # Bind series 0 to an axis named 'distance'.
            1 => { axis: 'distance' } # Bind series 1 to an axis named 'brightness'.
          },
          axes: {
            x: {
              distance: {label: 'parsecs'}, # Bottom x-axis.
              brightness: {side: 'top', label: 'apparent magnitude'} # Top x-axis.
            }
          },
  type: :bar, height: 400
}
bar_multiple_chart = Daru::View::Plot.new(bar_multiple_table.table, bar_multiple_options)
bar_multiple_chart.show_in_iruby


Out[67]:

In [ ]: